<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索框和添加商品按钮 -->
      <el-row>
        <el-col :span="6">
          <el-input 
            placeholder="请输入内容" 
            clearable
            v-model="queryInfo.query" 
            @clear="getGoodsList">
            <el-button 
            slot="append" 
            icon="el-icon-search"
            @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="3">
            <el-button 
            type="primary" 
            class="add_btn"
            @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品列表表格区域 -->
      <el-table
        :data="goodsList"
        border stripe
        style="width: 100%">
        <el-table-column
          type="index"
          label="#">
        </el-table-column>
        <el-table-column
          prop="goods_name"
          label="商品名称">
        </el-table-column>
        <el-table-column
          prop="goods_price"
          label="商品价格(元)"
          width="180">
        </el-table-column>
        <el-table-column
          prop="goods_weight"
          label="商品重量"
          width="180">
        </el-table-column>
        <el-table-column
          prop="upd_time"
          label="创建时间"
          width="180">
          <template slot-scope="scope">
            {{scope.row.upd_time | dateFomat}}
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="180">
          <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      background="">
    </el-pagination>
    </el-card>
  </div>
</template>

<script>
  export default{
    data(){
      return {
        // 商品列表数据
        goodsList:[],
        // 获取商品列表数据请求参数
        queryInfo:{
          query:'',
          pagenum:1,
          pagesize:10
        },
        // 总条数
        total:0
      }
    },
    created(){
      this.getGoodsList()
    },
    methods:{
      // 获取商品列表
      async getGoodsList(){
        const {data:res} = await this.$http.get('goods', { 
          params:this.queryInfo})
        if(res.meta.status !== 200){
          return this.$message.error('获取商品列表失败')
        }
        this.goodsList = res.data.goods
        this.total = res.data.total
        // console.log(res)
      },
      // 每页显示页码发生改变时
      handleSizeChange(val){
        this.queryInfo.pagesize = val
        this.getGoodsList()
      },
      // 当前页码发生改变时
      handleCurrentChange(val){
        this.queryInfo.pagenum = val
        this.getGoodsList()
      },
      // 点击添加商品按钮 跳转到添加商品页面
      goAddPage(){
        this.$router.push('/goods/add')
      }
    }
    
  }
</script>

<style lang="less" scoped>
.add_btn{
  margin-left:15px ;
}

</style>
